﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
// Copyright (C) 2022 kingerrobot
// 版权所有。 
//
// 文件名：TaskImpl
// 文件功能描述：
//
// 创建者：名字 (zzl)
// 时间：2022/1/26 16:49:20
//
// 修改人：
// 时间：
// 修改说明：
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion


namespace KingerRobot.MexicoSailunMDC.SpiService;

public class OrderImpl : IOrderService
{
    private readonly static IWmsSetting _WmsSetting = new BWmsSetting();
    private readonly static IBakWmsOutOrderMaster _BakWmsOutOrderMaster = new BBakWmsOutOrderMaster();
    private readonly static IWmsOutOrderMaster _WmsOutOrderMaster = new BWmsOutOrderMaster();
    private readonly static ITask _Task = new BTask();
    private readonly static IBakTask _BakTask = new BBakTask();
    private readonly static IWmsOutOrderSummary _WmsOutOrderSummary = new BWmsOutOrderSummary();
    // private readonly static IWmsOutOrderMaster _WmsOutOrderMaster = new BWmsOutOrderMaster();

    public int QuartzBakDeleteAndBakOrder()
    {
        var wmsSetting = _WmsSetting.GetWmsSettingByCode(ConstFields.QuartzBakAndDeleteOutOrder);
        int.TryParse(wmsSetting?.Value, out int hours);
        if (hours <= 0)
        {
            return 0;
        }

        var dateTime = DateTime.Now.AddHours(-hours);
        var result = _WmsOutOrderMaster.QuartzBakAndDeleteOutOrder(dateTime);

        return result;

    }

    public int DeleteBakOrder()
    {
        var wmsSetting = _WmsSetting.GetWmsSettingByCode(ConstFields.DeleteBakOrder);
        int.TryParse(wmsSetting?.Value, out int days);
        if (days <= 0)
        {
            return 0;
        }
        var dateTime = DateTime.Now.AddDays(-days);
        var result = _BakWmsOutOrderMaster.DeleteBakOrderByDateTime(dateTime);
        return result;
    }

    public int BakCleanNoTaskOutOrder()
    {

        return BakCleanNoTaskOutOrderV2();
        //var outTask = _Task.GetTaskByType((int)EnumTaskType.Exwarehouse);
        // emsTask = _Task.GetTaskByType((int)EnumTaskType.EMS);
        //var allTask = outTask.Union(emsTask);
        var allTask = _Task.GetHasSummaryTask();
        //拿到已经生成任务的请料单
        var date = DateTime.Now.AddMinutes(-5);
        //找到一分钟前的订单  防止在删除的过程中生成了任务
        var outSummaryList = _WmsOutOrderSummary.GetOutOrderSummaryHasTaskList().Where(t => t.CreateTime < date);
        var hasTaskSummaryIds = outSummaryList.Select(t => t.Id).ToList();
        //跟任务关联 找到存在任务的请料单
        var realTaskSummaryIds = outSummaryList.Join(allTask, f => f.Id, s => s.SummaryId, (f, s) => new
        {
            SummaryId = f.Id,
        }).Select(t => t.SummaryId).ToList();
        // var realTaskMasterIds =
        //在生成任务的请料单中排出存在任务
        var noTaskSummaryIdList = hasTaskSummaryIds.Except(realTaskSummaryIds);
        var result = 0;
        foreach (var summaryId in noTaskSummaryIdList)
        {
            var masterId = _WmsOutOrderSummary.GetOutOrderSummaryHasTaskList().Where(t => t.Id == summaryId).FirstOrDefault()?.MasterId ?? 0;
            var summaryList = _WmsOutOrderSummary.GetOutOrderSummaryByMasterId(masterId);

            //证明是单模请料 删除主从表信息
            if (summaryList.Count == 1)
            {    //删除从表
                _WmsOutOrderSummary.BakAndDeleteOutOrderSummary(summaryId ?? 0);
                //删除主表
                var master = _WmsOutOrderMaster.GetOutOrderMasterById(masterId);
                result = _WmsOutOrderMaster.BakAndDeleteOutOrderMaster(master);
            }
            else//双模请料  只删除对应的从表信息
            {
                result = _WmsOutOrderSummary.BakAndDeleteOutOrderSummary(summaryId ?? 0);
            }
        }
        return result;

    }

    public int BakCleanNoTaskOutOrderV2()
    {
        // 查找已生成任务订单
        var summarys = _WmsOutOrderSummary.GetOutOrderSummaryHasTaskList();

        // 查找是否有正在执行出库任务
        var allTask = _Task.GetHasSummaryTask();

        foreach (var summary in summarys)
        {
            var summaryId = summary.Id ?? 0;
            var masterId = summary.MasterId ?? 0;
            var remark = summary.RemarkMemo;
            var task = allTask.Where(t => t.SummaryId == summaryId).FirstOrDefault();
            if (task is null)
            {
                // 查找备份表是否有已删除任务
                var bakTask = _BakTask.GetBakTaskBySummaryId(summaryId);
                if(bakTask is not null || remark.IndexOf("任务已删除") > -1)
                {
                    _WmsOutOrderMaster.OutOrderFinishByBakTask(masterId, summaryId);
                }
                else
                {
                    _WmsOutOrderSummary.UpdateOrderSummaryRemark(new() { summaryId }, $"【任务已删除】{remark}");
                }
            }
        }

        return 1;
    }
}